<template>
  <div>
    <!-- data：表格中的数据 -->
    <!-- default-expand-all：默认展开所有节点 -->
    <!-- tree-props：树形结构的配置项 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" width="60"> </el-table-column>
      <el-table-column prop="title" label="标题" width="100"> </el-table-column>
      <el-table-column label="类型">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type === 1">目录</el-tag>
          <el-tag type="danger" v-else>菜单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图标">
        <template slot-scope="scope">
          <i v-if="scope.row.icon" :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="地址" width="100"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            circle
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- size-change: 修改每页显示的条数 -->
    <!-- current-change: 修改页码数 -->
    <!-- current-page: 当前页码数 -->
    <!-- page-sizes: 每页显示条数的选框 -->
    <!-- page-size: 每页显示多少条 -->
    <!-- layout: 要显示的控件 -->
    <!-- total: 数据的总条数 -->
    <!-- 分页的逻辑：1.总条数（total），后台知道，需要发请求；2.每页显示几条（size），前端知道；3.分成几页=total/size，有余数要向上取整；4.当前在看第几页（current-page） -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="size"
      layout="prev, pager, next, jumper, total"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getUserConut, getUserList } from "../../api";
export default {
  data() {
    return {
      // 表格中的数据
      tableData: [],
      // 每页显示几条
      size: 3,
      // 当前显示第几页
      currentPage: 1,
      // 总条数
      total: 1,
    };
  },
  created() {
    // 获取管理员总数
    this.getUserConutFn();
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPageData()
    },
    async getUserConutFn() {
      let res = await getUserConut();
      this.total = res.list[0].total;
      // 先获取总条数，计算分页后再获取分页后的数据
      this.getPageData()
    },
    // 获取分页后的数据
    async getPageData() {
      let res = await getUserList({
        page: this.currentPage,
        size: this.size,
      });
      this.tableData = res.list;
    },
    del(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确认按钮
        .then(async () => {
          // 真的删除
          let res = await delMenu(id);
          if (res.code === 200) {
            // code是200表示成功
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          } else {
            {
              this.$message({
                type: "warning",
                message: res.msg,
              });
            }
          }
          // 更新页面内容
          if (res.list.length) {
            this.tableData = res.list;
          } else {
            this.getTabelList();
          }
        })
        // 点击取消按钮
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log(id);
    },
    edit(data) {
      // 子传父
      this.$emit("edit", data);
    },
  },
};
</script>

<style>
</style>